//
//  UIViewController+LLNavigationBar.swift
//  LLNavigationBar
//
//  Created by 若望 on 2020/7/6.
//  Copyright © 2020 若望. All rights reserved.
//

import UIKit

/// 利用oc运行时添加分类属性的key值
private var kBarStyle: String = "kBarStyle"
private var kBarTintColor: String = "kBarTintColor"
private var kBarImage: String = "kBarImage"
private var kTintColor: String = "kTintColor"
private var kTitleTextAttributes: String = "kTitleTextAttributes"
private var kBarAlpha: String = "kBarAlpha"
private var kBarHidden: String = "kBarHidden"
private var kBarShadowHidden: String = "kBarShadowHidden"
private var kBackInteractive: String = "kBackInteractive"
private var kSwipeBackEnabled: String = "kSwipeBackEnabled"
private var kClickBackEnabled: String = "kClickBackEnabled"
private var kClomputedBarShadowAlpha: String = "kClomputedBarShadowAlpha"
private var kBackBarButtonItem: String = "kBackBarButtonItem"
private var kExtendedLayoutDidSet: String = "kExtendedLayoutDidSet"


extension UIViewController {
    /// 利用oc的进行时天剑分类的属性
    public var llBlackBarStyle: Bool {
        get {
            return self.llBarStyle == UIBarStyle.black
        }
        set(newValue) {
            if newValue {
                self.llBarStyle = UIBarStyle.black
            }else{
                self.llBarStyle = UIBarStyle.default
            }
        }
    }
    public var llBarStyle: UIBarStyle {
        get {
            if let value = objc_getAssociatedObject(self, &kBarStyle) as? UIBarStyle {
                return value
            }
            return UINavigationBar.appearance().barStyle
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBarStyle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llBarTintColor: UIColor? {
        get {
            if let value = objc_getAssociatedObject(self, &kBarTintColor) as? UIColor {
                return value
            }
            return nil
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBarTintColor, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llBarImage: UIImage? {
        get {
            if let value = objc_getAssociatedObject(self, &kBarImage) as? UIImage {
                return value
            }
            return nil
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBarImage, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llTintColor: UIColor {
        get {
            if let tintColor = objc_getAssociatedObject(self, &kTintColor) as? UIColor {
                return tintColor
            }else if let tintColor = UINavigationBar.appearance().tintColor {
                return tintColor
            }
            return UIColor.white
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kTintColor, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llTitleTextAttributes: [NSAttributedString.Key : Any]? {
        get {
            if let obj = (objc_getAssociatedObject(self, &kTitleTextAttributes) as? [NSAttributedString.Key : Any]) {
                return obj
            }
            let barStyle: UIBarStyle = self.llBarStyle
            if var attributes = UINavigationBar.appearance().titleTextAttributes {
                if attributes[NSAttributedString.Key.foregroundColor] == nil {
                    if (barStyle == UIBarStyle.black) {
                        attributes[NSAttributedString.Key.foregroundColor] = UIColor.white
                    } else {
                        attributes[NSAttributedString.Key.foregroundColor] = UIColor.black
                    }
                }
                return attributes
            }
            if barStyle == UIBarStyle.black {
                return [NSAttributedString.Key.foregroundColor: UIColor.white]
            } else {
                return [NSAttributedString.Key.foregroundColor: UIColor.black]
            }
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kTitleTextAttributes, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llBarAlpha: CGFloat {
        get {
            if self.llBarHidden {
                return 0.0
            }
            if let value = objc_getAssociatedObject(self, &kBarAlpha) as? CGFloat {
                return value
            }
            return 1.0
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBarAlpha, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llBarHidden: Bool {
        get {
            if let value = objc_getAssociatedObject(self, &kBarHidden) as? Bool {
                return value
            }
            return false
        }
        set(newValue) {
            if (newValue) {
                self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: UIView())
                self.navigationItem.titleView = UIView()
            } else {
                self.navigationItem.leftBarButtonItem = nil
                self.navigationItem.titleView = nil
            }
            objc_setAssociatedObject(self, &kBarHidden, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llBarShadowHidden: Bool {
        get {
            if let value = objc_getAssociatedObject(self, &kBarShadowHidden) as? Bool {
                return value
            }
            return false
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBarShadowHidden, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llBackInteractive: Bool {
        get {
            if let value = objc_getAssociatedObject(self, &kBackInteractive) as? Bool {
                return value
            }
            return true
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBackInteractive, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llSwipeBackEnabled: Bool {
        get {
            if let value = objc_getAssociatedObject(self, &kSwipeBackEnabled) as? Bool {
                return value
            }
            return true
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kSwipeBackEnabled, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llClickBackEnabled: Bool {
        get {
            if let value = objc_getAssociatedObject(self, &kClickBackEnabled) as? Bool {
                return value
            }
            return true
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kClickBackEnabled, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    // computed
    public var llComputedBarShadowAlpha: CGFloat {
        get {
            return self.llBarShadowHidden ? 0.0 : self.llBarAlpha
        }
    }
    public var llComputedBarTintColor: UIColor? {
        get {
            if self.llBarImage != nil {
                return nil
            }
            var color: UIColor? = self.llBarTintColor
            if color == nil {
                if (UINavigationBar.appearance().backgroundImage(for: .default) != nil) {
                    return nil
                }
                if let tintColor = UINavigationBar.appearance().barTintColor {
                    color = tintColor
                }else {
                    color = (UINavigationBar.appearance().barStyle == UIBarStyle.default) ? UIColor.init(red: 247/255.0, green: 247/255.0, blue: 247/255.0, alpha: 0.8) : UIColor.init(red: 28/255.0, green: 28/255.0, blue: 28/255.0, alpha: 0.729)
                }
            }
            return color
        }
    }
    public var llComputedBarImage: UIImage? {
        get {
            let image: UIImage? = self.llBarImage;
            if image == nil {
                if self.llBarTintColor != nil {
                    return nil
                }
                return UINavigationBar.appearance().backgroundImage(for: .default)
            }
            return image
        }
    }
    
    // 这个属性是内部使用的
    public var llBackBarButtonItem: UIBarButtonItem? {
        get {
            if let value = objc_getAssociatedObject(self, &kBackBarButtonItem) as? UIBarButtonItem {
                return value
            }
            return nil
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kBackBarButtonItem, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var llExtendedLayoutDidSet: Bool {
        get {
            if let value = objc_getAssociatedObject(self, &kExtendedLayoutDidSet) as? Bool {
                return value
            }
            return false
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kExtendedLayoutDidSet, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public func llSetNeedsUpdateNavigationBar() {
        if let navigationC = self.navigationController as? LLNavigationController {
            navigationC.updateNavigationBarForViewController(self)
        }
    }
}
